package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.data.AtofModuleConstants;
import org.testis.gui.mvc.model.testdesign.DbConfigModel;
import org.testis.gui.mvc.view.testdesign.DbConfigView;
import org.testis.utils.PropertyFileProvider;


/**
 *
 * @author nurhat
 */
public class DbConfigController extends AbstractController
{

    public static final String CHANGE_DATABASE_URL = "DataBaseUrl";
    public static final String CHANGE_USERNAME = "UserName";
    public static final String CHANGE_PASSWORD = "Password";
    public static final String CHANGE_CATALOG_NAME = "CatalogName";
    public static final String CHANGE_ACTIVEMQ_URL = "ActiveMqUrl";
    public static final String SHOW_DBCONFIG_VIEW = "DbConfigVisibility";


    public DbConfigController()
    {
        super();
    }


    public void startLoader()
    {
        initialize();
        setModelProperty(SHOW_DBCONFIG_VIEW, true);
    }


    public void operationClose()
    {
        cleanAll();
    }


    public void initialize()
    {
        PropertyFileProvider propFile = new PropertyFileProvider();
        boolean isLoaded = propFile.loadPropertiesFile(AtofModuleConstants.ATOF_VENDOR_HOME +
                                                       "/" +
                                                       "database.props",
                                                       false);

        addModel(new DbConfigModel());
        DbConfigView myView = new DbConfigView(TestDesignControllerList.
                                               newInstance().
                                               getParentFrame(),
                                               true,
                                               isLoaded);
        addView(myView);
        myView.pack();

        if (isLoaded) {
            operationSetDataBaseUrl(propFile.getProperty("DATABASE_URL"));
            operationSetUsername(propFile.getProperty("DATABASE_USERNAME"));
            operationSetPassword(propFile.getProperty("DATABASE_PASSWORD"));
            operationSetCatalogName(propFile.getProperty("DATABASE_CATALOGNAME"));
            operationSetActiveMqUrl(propFile.getProperty("ACTIVEMQ_URL"));
        }
        else {
            operationSetDataBaseUrl("localhost");
            operationSetUsername("root");
            operationSetPassword("12345");
            operationSetCatalogName("atof");
            operationSetActiveMqUrl("localhost");
        }

    }


    public void operationSaveChanges(String dbUrl,
                                     String userName,
                                     String password,
                                     String catalogName,
                                     String activemqUrl)
    {
        PropertyFileProvider propFile = new PropertyFileProvider();
        propFile.loadPropertiesFile(AtofModuleConstants.ATOF_VENDOR_HOME + "/" +
                                    "database.props",
                                    false);

        propFile.putProperty("DATABASE_URL",
                             dbUrl,
                             "");
        propFile.putProperty("DATABASE_USERNAME",
                             userName,
                             "");
        propFile.putProperty("DATABASE_PASSWORD",
                             password,
                             "");
        propFile.putProperty("DATABASE_CATALOGNAME",
                             catalogName,
                             "");
        propFile.putProperty("ACTIVEMQ_URL",
                             activemqUrl,
                             "");
    }


    public void operationSetCatalogName(String input)
    {
        setModelProperty(CHANGE_CATALOG_NAME,
                         input);
    }


    public String operationGetCatalogName()
    {
        return (String) getModelProperty(CHANGE_CATALOG_NAME);
    }


    public void operationSetActiveMqUrl(String input)
    {
        setModelProperty(CHANGE_ACTIVEMQ_URL,
                         input);
    }


    public String operationGetActiveMqUrl()
    {
        return (String) getModelProperty(CHANGE_ACTIVEMQ_URL);
    }


    public void operationSetDataBaseUrl(String input)
    {
        setModelProperty(CHANGE_DATABASE_URL,
                         input);
    }


    public String operationGetDataBaseUrl()
    {
        return (String) getModelProperty(CHANGE_DATABASE_URL);
    }


    public void operationSetUsername(String input)
    {
        setModelProperty(CHANGE_USERNAME,
                         input);
    }


    public String operationGetUsername()
    {
        return (String) getModelProperty(CHANGE_USERNAME);
    }


    public void operationSetPassword(String input)
    {
        setModelProperty(CHANGE_PASSWORD,
                         input);
    }


    public String operationGetPassword()
    {
        return (String) getModelProperty(CHANGE_PASSWORD);
    }
}
